<?php
include_once ROOT . '/utils/Notifier.php';
if (empty($_SESSION['user'])) {
    $notification = new Notifier('Be connected', 'To buy a robot you need to be connected.');
    echo $template->render('user/inscription.php', array('titleInscr' => 'Inscription',
        'route' => getRoute('inscriptionpost'), 'buttonTxt' => 'Register', 'notifications' => array($notification)));
    exit();
}
if (empty($_SESSION['cart']['products'])) {
    $notification = new Notifier('Empty cart', 'Your cart is empty.');
    echo $template->render('cart/cartDetailsView.php', array('notifications' => array($notification)));
    exit();
}
include_once ROOT . '/model/dao/ProductDao.php';
$productDao = new ProductDao();
foreach ($_SESSION['cart']['products'] as $idProduct => $infoProduct) {
    $product = $productDao->findById($idProduct);
    $number = $product->getNumber() - $infoProduct['qty'];
    $product->setNumber($number);
    $productDao->update($product);
}
$_SESSION['cart'] = null;
$notification = new Notifier('Bought', 'You just bought all your robots, congratulation');
echo $template->render('static/welcome.php', array('notifications' => array($notification)));
